package com.service.impl;

import com.constant.Constant;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageInfo;
import com.mapper.LoginMapper;
import com.service.LoginService;
import com.util.AjaxResult;
import com.util.CommonUtil;
import com.vo.PageVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    LoginMapper loginMapper;

    @Override
    public List<User> LoginISNameAndPassWord(String login_name, String password) {
      return loginMapper.loginByNameAndPassword(login_name,password);
    }

    @Override
    public int updateUserS(User user) throws ServiceException {
        return loginMapper.updateUser(user);
    }

    @Override
    public void upLoginDateS(String login_name) throws ServiceException {

    }

    //后台管理员登录
    @Override
    public User findUserBy(User user,HttpServletRequest request) throws Exception {
        User user1 = loginMapper.selUserBy(user);
        //判断用户权限
        if(user1.getRole().equals("admin")){  //后台只有管理员才能登陆
            request.getSession().setAttribute(Constant.SESSION_USER, user);
            AjaxResult.isOk();
        }
        else{
            AjaxResult.isFail("用户权限不足");
        }
        return loginMapper.selUserBy(user);
    }

}
